package org.kl.bpm.operation;

import java.util.List;

import org.activiti.engine.impl.interceptor.CommandContext;
import org.kl.bf.utils.ApplicationContextHelper;
import org.kl.bpm.entity.conf.BpmTaskConf;
import org.kl.bpm.service.conf.BpmTaskConfService;

/**
 * 配置任务负责人.
 */
public class ConfAssigneeOperation extends AbstractOperation<String> {
	public static final String OPERATION_TASK_DEFINITION_KEYS = "taskDefinitionKeys";
	public static final String OPERATION_TASK_ASSIGNEES = "taskAssignees";
	public static final String OPERATION_BUSINESS_KEY = "businessKey";

	public String execute(CommandContext commandContext) {
		List<String> taskDefinitionKeys = getParameterValues("taskDefinitionKeys");
		List<String> taskAssignees = getParameterValues("taskAssignees");
		String businessKey = new SaveDraftOperation().execute(getParameters());
		BpmTaskConfService bpmTaskConfService = getBpmTaskConfService();

		if (taskDefinitionKeys != null) {
			// 如果是从配置任务负责人的页面过来，就保存TaskConf，再从草稿中得到数据启动流程
			int index = 0;

			for (String taskDefinitionKey : taskDefinitionKeys) {
				String taskAssignee = taskAssignees.get(index++);
				BpmTaskConf bpmTaskConf = new BpmTaskConf();
				bpmTaskConf.setBusinessKey(businessKey);
				bpmTaskConf.setTaskDefinitionKey(taskDefinitionKey);
				bpmTaskConf.setAssignee(taskAssignee);
				bpmTaskConfService.saveBpmTaskConf(bpmTaskConf);
			}
		}

		return businessKey;
	}

	public BpmTaskConfService getBpmTaskConfService() {
		return ApplicationContextHelper.getBean(BpmTaskConfService.class);
	}
}
